package com.drop.service.impl;

import com.drop.common.MyComment;
import com.drop.common.States;
import com.drop.dao.CommentDao;
import com.drop.dao.CommonDao;
import com.drop.entity.*;
import com.drop.service.CommentService;
import com.drop.service.OrderService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class CommentServiceImpl implements CommentService {
	private Log log = LogFactory.getLog(this.getClass());

	@Autowired
	private CommonDao commonDao;
	
	@Autowired
	private CommentDao commentDao;

	@Autowired
	private OrderService orderService;

	@Override
	public List<MyComment> showComments(int id) {
		List<Comment> comments =  (List<Comment>)(List)commonDao.getAllByOtherId(Comment.class,id,"goods.goodsId");
		//log.info(comments);
		List<MyComment> myComments = new ArrayList<>();
		MyComment myComment;
		for(int i=0; i<comments.size(); i++) {
			myComment = new MyComment();
			myComment.setName(comments.get(i).getUser().getName());
			myComment.setContent(comments.get(i).getContent());
			myComment.setTime(comments.get(i).getCommentTime());
			myComment.setNumber(comments.size()+"");
			myComments.add(myComment);
		}
		return myComments;
	}

	@Override
	public Boolean addComment(String[] comment, int orderid, String uid,String[] gids) throws Exception {
		Order order = (Order) commonDao.getById(Order.class, orderid);
		User user = (User) commonDao.getById(User.class, Integer.parseInt(uid));
		Set<Childorder> childorders = order.getChildorders();
		orderService.updateOrder(orderid, States.完成.getValue(),order.getAddress());
		return 	commentDao.saveComment(comment,orderid,uid,gids);
	}

	@Override
	public List<Comment> getComment(int userid) {
		return commentDao.getComment(userid);
	}


}
